from selenium.webdriver.support.expected_conditions import presence_of_element_located
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium import webdriver
import pyautogui as pi
import xlrd
import time
url_address = '/member/login/executeLogin.htm'
url_uat = 'http://61.187.51.25:8084'
url_dev = 'http://61.187.51.25:8800'
url_prod = 'https://www.holdcg.com'
url_local = 'http://localhost:6850'
PASSWORD = 's#f]A4@_a;M,d-c[lPn)'

class Setting:
    def __init__(self):
        path_file = "./login_user_accounts.xls"
        workbook = xlrd.open_workbook(path_file)
        worksheet1 = workbook.sheet_by_name('sheet1')
        self.worksheet1 =  worksheet1
        self.log_file=open("users_login_log.txt","a")
    def debug_print(self,s):
        self.write_log(s)
    def write_log(self,s):
        time_now = time.localtime()
        date_now = time.strftime('%Y-%m-%d',time_now)
        format_log_str = "%s ---> %s \n" % (date_now,s)
        print(format_log_str)
        self.log_file.write(format_log_str)
    def getFlag(self):
        s = str(self.worksheet1.cell_value(1,4))
        if s.upper() == "F":
            return False
        elif s.upper() == "C":
            return True
    def getUrl(self):
        s = str(self.worksheet1.cell_value(1,2))
        if s.upper() == 'UAT':
            return url_uat + url_address
        if s.upper() == 'DEV':
            return url_dev + url_address
        if s.upper() == 'PROD':
            return url_prod + url_address
        if s.upper() == 'LOCAL':
            return url_local + url_address
        if s.upper() == 'CUSTOM':
            return  "http://"+self.getCustomUrl()+url_address
        
    def getNumber(self):
        number1 = str(int(self.worksheet1.cell_value(0,1)))
        return number1
    def getCustomUrl(self):
        customurl1 = str(self.worksheet1.cell_value(1,3))
        return customurl1
    def getAccount(self,i):
        return str(self.worksheet1.cell_value(i,0))


settings = Setting()
number1 = settings.getNumber()
url1 = settings.getUrl()
flag1  = settings.getFlag()

def usersLogin(driver,account1,url):
    settings.debug_print("%s: account login" %account)
    settings.debug_print("%s: url :" %url)
    driver.get(url)
    locationLogin = (By.LINK_TEXT, '立即登录')
    try:
        WebDriverWait(driver, 20, 0.5).until(presence_of_element_located(locationLogin))
    except Exception as e:
        #print(e)
        settings.debug_print("%s : login error:"%e)
        pi.alert('登录界面加载失败----')
    driver.find_element(By.ID,"username").send_keys(account1)
    driver.find_element(By.ID,"xstr").send_keys(PASSWORD)
    driver.find_element(By.LINK_TEXT,"立即登录").click()
    try:
        driver.find_element(By.ID,"popup_ok").click()
    except Exception as e:
        settings.debug_print("%s Not found button to click: "%e)
        #print("Not found ok button")
if __name__ == '__main__':
    if flag1:
        options = webdriver.ChromeOptions()
        settings.debug_print("chrome webdriver is up")
        options.add_argument('--args --disable-web-security --start-maximized --incognito -lang=zh-CN')
        options.add_experimental_option('useAutomationExtension', False)
        options.add_experimental_option("excludeSwitches", ['enable-automation'])
        dl = []
        for i in range(int(number1)):
            account = settings.getAccount(i)
            dl.append(webdriver.Chrome(options=options))
            usersLogin(dl[i],account,url1)
    else:
        options = Options()
        firefox_profile = webdriver.FirefoxProfile()
        settings.debug_print("firefox webdriver is up")
        #firefox_profile.set_preference("browser.privatebrowsing.autostart", True)
        options.add_argument("--incognito")
        #browser = webdriver.Firefox(options=options, firefox_profile=firefox_profile)
        dl = []
        for i in range(int(number1)):
            account = settings.getAccount(i)
            driver = webdriver.Firefox(options=options)
            driver.maximize_window()
            dl.append(driver)
            usersLogin(dl[i],account,url1)
        #driver.maximize_window()
